//
//  main.swift
//  C150805_libxml2
//
//  Created by yao_yu on 15/8/5.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import Foundation

print("Hello, World!")

var xmlDoc: XMLDocument?
var nodes: [XMLNode]?
var data: NSData?

//data = NSData(contentsOfURL: NSURL(string: "http://www.baidu.com")!)
//
//xmlDoc = XMLDocument(htmlData: data!)//, encoding: NSASCIIStringEncoding)
//nodes = xmlDoc?.xPath("//body")
//print(nodes?.first?.content)
//
//data = NSData(contentsOfURL: NSURL(string: "http://news.sina.com.cn/")!)
//xmlDoc = XMLDocument(htmlData: data!)//, encoding: NSASCIIStringEncoding)
//if let nodes = xmlDoc?.xPath("//a") {
//    for node in nodes {
//        print((node.attributes, node.content))
//    }
//}
//http://data.eastmoney.com/rzrq/detail/601857.html

var encode:NSStringEncoding
encode = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue))

//encode = NSISOLatin1StringEncoding


data = NSData(contentsOfURL: NSURL(string: "http://data.eastmoney.com/rzrq/detail/601857.html")!)
//print(data)
var str = NSString(data: data!, encoding: encode)
str = str?.stringByReplacingOccurrencesOfString("; charset=gb2312\"", withString: "")
//print(str)
let bb = true
encode = NSUTF8StringEncoding
data = str?.dataUsingEncoding(encode)
if bb {
    xmlDoc = XMLDocument(htmlData: data!, encoding: encode)
//    print(xmlDoc?.rootNode?.content)
    if let nodes = xmlDoc?.xPath("//script") {
        for node in nodes {
            print(("=== \(node.content!)"))
        }
    }
}
